package xFGE.Timer
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	public class xFPS extends Sprite
	{
		private var _lastTime:int;
		
		private var _fps:int;
		
		private var _frameCount:int; 
		
		private	var _delayTime : int;
		
		private static var _isInit:Boolean = false;
		
		private static var _myInstance:xFPS;
		
		
		public function xFPS()
		{
			//TODO: implement function
			if(_myInstance){
				throw new Error("请用getInstance获取实例");
			}
		
		}

		public function initFPS():void
		{
			if(!_isInit){
				_lastTime = getTimer();
				this.addEventListener(Event.ENTER_FRAME,_onEnterFrame);
				_isInit = true;
			}
			
		}
		
		private function _onEnterFrame(eve:Event):void
		{
			_delayTime = getTimer() - _lastTime;
			if(_delayTime >= 1000){
				_fps = _frameCount;
				_frameCount = 0;
				_lastTime +=_delayTime;
			}else _frameCount++;
		}
		
		public static function getInstance():xFPS
		{
			if(_myInstance == null) _myInstance = new xFPS();
			return _myInstance;
		}
		
		public function get fps():int
		{
			return _fps;
		}
		
		public function get delayTime():int
		{
			return _delayTime;
		}
		
	}
}